//
//  BGComposeToolBar.swift
//  WeiboSwift
//
//  Created by 战场原黑仪 on 16/7/28.
//  Copyright © 2016年 战场原黑仪. All rights reserved.
//

import UIKit

// 按钮枚举
enum BGComposeToolbarViewType: Int{
    // 图片
    case Picture = 1001
    // @
    case Mention = 1002
    // #
    case Trend = 1003
    // 表情
    case Emoticon = 1004
    // +
    case Add = 1005
}

class BGComposeToolBar: UIView {
    
    // 定义一个闭包
    var closure:((type: BGComposeToolbarViewType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 监听方法
    @objc func buttonClick(button: UIButton){
        // 执行闭包
        closure?(type: BGComposeToolbarViewType(rawValue: button.tag)!)
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        // 添加控件
        let pictureButton = addChildButton("compose_toolbar_picture",type: .Picture)
        let mentionButton = addChildButton("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButton("compose_trendbutton_background",type: .Trend)
        let emoticonButton = addChildButton("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = addChildButton("compose_add_background",type: .Add)
        // 添加约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton)
        }
        
        emoticonButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton.snp_trailing)
        }
        
    }
    
    /// 抽取出创建button公共方法
    ///
    /// - parameter imgName: 图片名
    ///
    /// - returns: button
    private func addChildButton(imgName: String, type: BGComposeToolbarViewType) -> UIButton{
        let button = UIButton()
        // rawValue 通过rawValue 把枚举名 转成枚举int数值
        button.tag = type.rawValue
        // 添加点击事件
        button.addTarget(self, action: #selector(buttonClick), forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named:imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }

    
}
